from flask import Blueprint, request, jsonify, session
import api.friend_api as friend_api 
import api.user_api as user_api 

friend = Blueprint('friend',__name__)

# 好友模块 friend/list  
# 路由配置


# 好友列表
@friend.route('/list', methods=['get'])
def friend_list():
    #好友列表
    list = []
    if session.get('id'):
        id = session.get('id')
        id_list = friend_api.Friend_List(id)
        for item in id_list:
            user = user_api.User_Find_Id(item['id2'])
            list.append(user[0])
    return jsonify(list)


# 添加好友
@friend.route('/add', methods=['get'])
def friend_add():
    data={'state':''}
    if session.get('id'):
        id1 = str(session.get('id'))
        id2 = request.args.get('id')
        if id1 == id2:
            data['state']="本人"
        elif friend_api.Friend_Check(id1, id2):
            data['state']="已为好友"
        else:
            friend_api.Friend_Add(id1, id2)
            data['state']="添加成功"
    else:
        data['state']="未登录"
    return jsonify(data)


# 删除好友
@friend.route('/delete', methods=['get'])
def friend_delete():
    id1 = request.args.get('id1')
    id2 = request.args.get('id2')
    if friend_api.Friend_Check(id1, id2):
        friend_api.Friend_Delete(id1, id2)
        return "删除好友"
    else:
        return "非好友"





    
